
package com.showdor.springboot;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: lijb6
 * @Version 1.0
 */
@RestController
public class GroupKafkaProducer {
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    @GetMapping("/kafka/test/{msg}")
    public void sendMessage(@PathVariable("msg") String msg) {
        kafkaTemplate.send("netcross1", msg);
    }

    @GetMapping("/kafka/{topic}/{num}")
    public void sendMsg(@PathVariable("topic") String topic, @PathVariable Integer num) {
        List<JSONObject> list = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
            String str="{\"data\":[{\"MotorVehicleID\":\""+i+"\",\"InfoKind\":\"1\",\"SourceID\":\"1\",\"TollgateID\":\"1\",\"DeviceID\":\"1\",\"StorageUrl1\":\"浙A123457_01.jpg\",\"StorageUrl2\":\"浙A123457_01.jpg\",\"StorageUrl3\":\"1\",\"StorageUrl4\":\"1\",\"StorageUrl5\":\"1\",\"MarkTime\":\"\",\"LaneNo\":\"1\",\"PlateClass\":\"1\",\"PlateColor\":\"1\",\"PlateNo\":\"浙A"+i+"\",\"PlateNoAttach\":\"1\",\"PlateDescribe\":\"1\",\"Speed\":\"1.0\",\"Direction\":\"1\",\"UsingPropertiesCode\":\"1\",\"VehicleClass\":\"1\",\"VehicleBrand\":\"1\",\"VehicleModel\":\"1\",\"VehicleColor\":\"1\",\"create_time\":\"2023-07-04 10:09:55\",\"PassTime\":\"20230713190307\",\"NameOfPassedRoad\":\"1\",\"ThirdId\":\"1\"}]}";
            JSONObject jo = new JSONObject();
            JSONObject jo2 = new JSONObject();
            jo2.put("id", i);
            jo.put("order", jo2);
            //jo.put("order_id", i + new Random().nextInt(100));
            jo.put("order_date", new Date());
            jo.put("customer_name", i==10?"test":"customer_name " + i);
            jo.put("price", new Random().nextDouble());
            jo.put("product_id", 106);
            jo.put("order_status", 1);
            list.add(jo);
            //向kafka的big_data_topic主题推送数据
            kafkaTemplate.send(topic, jo.toJSONString());
            // kafkaTemplate.send(topic, JSONObject.toJSONString(list));
        }
        //System.out.println(JSONObject.toJSONString(list));
        //kafkaTemplate.send(topic, JSONObject.toJSONString(list));
    }

    @GetMapping("/kafka/veh/{topic}/{num}")
    public void sendVehMsg(@PathVariable("topic") String topic, @PathVariable Integer num) {
        List<JSONObject> list = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
             String str="{\"data\":[{\"MotorVehicleID\":\""+i+"\",\"InfoKind\":\"1\",\"SourceID\":\"1\",\"TollgateID\":\"1\",\"DeviceID\":\"1\",\"StorageUrl1\":\"浙A123457_01.jpg\",\"StorageUrl2\":\"浙A123457_01.jpg\",\"StorageUrl3\":\"1\",\"StorageUrl4\":\"1\",\"StorageUrl5\":\"1\",\"MarkTime\":\"\",\"LaneNo\":\"1\",\"PlateClass\":\"1\",\"PlateColor\":\"1\",\"PlateNo\":\"浙A"+i+"\",\"PlateNoAttach\":\"1\",\"PlateDescribe\":\"1\",\"Speed\":\"1.0\",\"Direction\":\"1\",\"UsingPropertiesCode\":\"1\",\"VehicleClass\":\"1\",\"VehicleBrand\":\"1\",\"VehicleModel\":\"1\",\"VehicleColor\":\"1\",\"create_time\":\"2023-07-04 10:09:55\",\"PassTime\":\"20230713190307\",\"NameOfPassedRoad\":\"1\",\"ThirdId\":\"1\"}]}";
            //向kafka的big_data_topic主题推送数据
            kafkaTemplate.send(topic, str);
            // kafkaTemplate.send(topic, JSONObject.toJSONString(list));
        }
        //System.out.println(JSONObject.toJSONString(list));
        //kafkaTemplate.send(topic, JSONObject.toJSONString(list));
    }
}
